package org.wendu.wdoa.security.api;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.wendu.wdoa.common.Result;
import org.wendu.wdoa.common.page.Page;
import org.wendu.wdoa.security.dto.MenuDto;
import org.wendu.wdoa.security.dto.RoleQueryDto;
import org.wendu.wdoa.security.service.RoleService;
import org.wendu.wdoa.security.model.Role;

import javax.validation.constraints.NotNull;
import java.util.List;

@Validated
@RestController
@RequestMapping("/role")
public class RoleController {

    @Autowired
    private RoleService roleService;

    /*
        Query String（查询字符串方式，url后通过？引出的参数） 适用于任何请求
        请求体（payload方式json请求体）适用post、delete、put等请求


     */
    @GetMapping("")
    public Result<Page<Role>> roleList(RoleQueryDto dto){
         Page<Role> page = roleService.getRolePage(dto);
         return Result.OK(page);
    }

    /**
     *
     * @param ro_id 角色编号，前端以Query String方式以ro_id为名称的参数
     * @param funIds 角色拥有的权限编号，以请求体（json）方式传递
     * @return
     */
    @PutMapping("/fun")
    public Result setRoleFuns(
            @NotNull(message = "角色编号不得为空！") Integer ro_id,
            @NotNull(message = "权限编号不得为空！") @RequestBody Integer[] funIds){

        roleService.setRoleFuns(ro_id,funIds);
        return Result.OK();

    }

    @GetMapping("/fun/{roleId}")
    public Result<List<MenuDto>> roleFunList(@PathVariable Integer roleId){
        List<MenuDto> menuList = roleService.getRoleFunList(roleId);
        return Result.OK(menuList);
    }

    @DeleteMapping("")
    public Result xxx(@RequestBody Integer[] ids){
        //
        //return Result.OK();
        return Result.err(Result.CODE_ERR_BUSINESS, "删除失败！");
    }




}
